<template>
  <!-- 房间表 -->
  <div class="app-container">
    <div class="app-wrap">
      <search-table ref="table" :table-loading="listLoading" :table-data="dataList" @query="getDataList(true)" @reset="refresh">
        <template v-slot:row>
          <el-form-item label="游戏类型:" label-width="90px">
            <el-select v-model="listQuery.game_id" placeholder="请选择游戏类型" filterable >
              <el-option label="全部" :value="0"></el-option>
              <el-option :label="item.label" :value="item.value" v-for="(item, index) in GAME_TYPE" :key="index"></el-option>
              <el-option label="老虎机" :value="3"></el-option>
            </el-select>
          </el-form-item>
        </template>
        <template v-slot:actions>
          <el-button type="primary" v-permission="'room.add'" size="mini" icon="el-icon-plus"
          :disabled="!listQuery.game_id" @click="isShowDetails = true,checkedItem = null">新增</el-button>
          <el-button type="info" size="mini" icon="el-icon-refresh" @click="getDataList">刷新</el-button>
          <el-button size="mini" v-permission="'room.robotAdd'" :disabled="!checkedItem" type="success" @click.stop="isRobotShow = true">新增机器人</el-button>
        </template>
        <template>
          <el-table-column align="center" type="index" width="50">
            <template slot-scope="scope">
              <el-radio v-model="scope.row.isChecked" :label="true" @change="handleCurrentChange(scope.row)">{{ "" }}</el-radio>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" prop="id" label="ID" width="70" /> -->
          <el-table-column align="center" prop="a_room_remark" label="备注" width="150"/>
          <el-table-column align="center" prop="a_user_real" label="游戏类型" width="120">
            <template slot-scope="scope">
              {{gameType(scope.row.game_id)}}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_robot_bigkill" label="大杀率触发概率" width="140"/>
          <el-table-column align="center" prop="a_enter_need" label="进入房间要求" width="120">
            <template slot-scope="scope">
              <el-tag :type="scope.row.a_enter_need?'success':'info'">{{ scope.row.a_enter_need?'充值':'暂无需求' }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_user_real" label="房间对局" width="120">
            <template slot-scope="scope">
              <el-tag :type="scope.row.a_user_real?'success':'danger'">{{ scope.row.a_user_real?'真人玩家':'机器人对局' }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_gold_limit_time" label="进入房间门槛倍数" width="140"/>
          <el-table-column align="center" prop="a_kick_time" label="踢出房间倍数" width="120"/>
          <el-table-column align="center" prop="a_slots_exchange" label="卢比筹码兑换比例" width="150">
            <template slot-scope="scope">
              <span>{{ $public.keepFour(scope.row.a_slots_exchange/100) }}%</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_gold_change" label="底金区间变化" width="120"/>
          <el-table-column align="center" prop="a_gold_boot" label="底金/每分额度" width="120"/>
          <el-table-column align="center" prop="a_gold_boot_low" label="底金区间下限值" width="120"/>
          <el-table-column align="center" prop="a_gold_boot_up" label="底金区间上限值" width="120">
            <template slot-scope="scope">
              <span v-if="scope.row.a_gold_boot_up!=-1">{{ scope.row.a_gold_boot_up }}</span>
              <span v-else>无限制</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_user_limit" label="房间最大真人数" width="120"/>
          <el-table-column align="center" prop="a_recharge_radio" label="充值额度计算倍数" width="140"/>
          <el-table-column align="center" prop="a_robot_join" label="机器人直接注入概率" width="150">
            <template slot-scope="scope">
              <span>{{ $public.keepFour(scope.row.a_robot_join/100) }}%</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_robot_join_num" label="机器人直接注入数量" width="150"/>
          <el-table-column align="center" prop="a_robot_bring" label="机器人进场携带金额" width="150">
            <template slot-scope="scope">
              <span>{{ scope.row.a_robot_bring[0] }}--{{ scope.row.a_robot_bring[1]}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_robot_match" label="机器人匹配注入概率" width="150">
            <template slot-scope="scope">
              <span>{{ $public.keepFour(scope.row.a_robot_match/100) }}%</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_robot_unkill" label="机器人玩家赢局概率" width="150">
            <template slot-scope="scope">
              <span>{{ $public.keepFour(scope.row.a_robot_unkill/100) }}%</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_robot_kill" label="机器人杀率触发概率" width="150">
            <template slot-scope="scope">
              <span>{{ $public.keepFour(scope.row.a_robot_kill/100) }}%</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_robot_exit_num" label="机器人最大退出次数" width="150"/>
          <el-table-column align="center" prop="a_slots_rtp" label="初始RTP" width="100">
            <template slot-scope="scope">
              <span>{{ $public.keepFour(scope.row.a_slots_rtp/100) }}%</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_bind_change" label="非杀局闷牌轮次概率" width="400" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.a_bind_change">
                <span v-for="(item,index) in scope.row.changeClose" :key="index">
                  第【{{item.min}}】轮—第【{{item.max}}】轮,概率{{item.chance/100}}%;
                </span>
              </span>
              <span v-else> -- </span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_robot_newbie" label="初入进入房间杀率配置" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.a_robot_newbie">
                <span>杀率{{ $public.keepFour(scope.row.newbie[0]/100) }}%,</span>
                <span>大杀率{{ $public.keepFour(scope.row.newbie[1]/100) }}%,</span>
                <span>机器人强制弃牌时玩家押注占携带百分比{{ $public.keepFour(scope.row.newbie[2]/100) }}%</span>
              </span>
              <span v-else> -- </span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_robot_rmkillcontrol" label="杀率局时Rm机器人胡牌控制" width="240" show-overflow-tooltip >
            <template slot-scope="scope">
              <span v-if="scope.row.a_robot_rmkillcontrol">
                <span v-for="(item,index) in scope.row.rmkill" :key="index">
                  第【{{item.round}}】轮,概率{{ $public.mathNumberMultiply($public.mathNumberDivide(item.chance,scope.row.rmkillNum),100) }}%;
                </span>
              </span>
              <span v-else> -- </span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_user_rmkillcontrol" label="杀率局时Rm玩家控制" width="400" show-overflow-tooltip >
            <template slot-scope="scope">
              <span v-if="scope.row.a_user_rmkillcontrol">
                <span>烂牌【<span class="colorRed">{{ scope.row.userRmkill[0]/100}}%</span>】,</span>
                <span>听牌时让摸胡牌【<span class="colorRed">{{ scope.row.userRmkill[1]/100}}%</span>】,</span>
                <span>玩家拥有1st和2nd时1到2轮杀【<span class="colorRed">{{ scope.row.userRmkill[2]/100}}%</span>】</span>
              </span>
              <span v-else> -- </span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_user_newer" label="新手配置" width="500" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.a_user_newer">
                新手送分模式每日次数【{{scope.row.newer[0]}}】
                触发新手送分模式额度【{{scope.row.newer[1]}}】
                新手送分模式杀率【{{$public.keepFour(scope.row.newer[2]/100)}}%】
                新手送分模式大杀率【{{$public.keepFour(scope.row.newer[3]/100)/100}}%】
                解除新手送分模式额度【{{scope.row.newer[4]}}】
                新手正常模式杀率【{{$public.keepFour(scope.row.newer[5]/100)}}%】
                新手正常模式大杀率【{{$public.keepFour(scope.row.newer[6]/100)}}%】
              </span>
              <span v-else> -- </span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_user_newerchangekill" label="新手超额配置" width="400" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.a_user_newerchangekill">
                <span v-for="(item,index) in scope.row.newerchangekill" :key="index">
                  下限额度【{{item.min}}】-上限额度【{{item.max}}】,概率{{item.chance/100}}%;
                </span>
              </span>
              <span v-else> -- </span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="a_user_set" label="A豹配置" width="500" show-overflow-tooltip >
            <template slot-scope="scope">
              <span v-if="scope.row.a_user_set">
                <span v-for="(item,index) in scope.row.leopard" :key="index">
                  玩家携带触发底注倍数下限【{{item.min}}】-上限【{{item.max}}】,触发概率{{item.chance}}】,充值额度计算倍数{{item.multiple}}】;
                </span>
              </span>
              <span v-else> -- </span>
            </template>
          </el-table-column>
          <el-table-column align="left" fixed="right" prop="operation" label="操作" width="150">
            <template slot-scope="scope">
              <el-tooltip effect="dark" v-permission="'room.edit'" content="编辑" placement="top-start">
                <el-button size="mini" type="warning" @click.stop="editItem(scope.row)"><i class="el-icon-edit-outline" /></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" v-permission="'room.delete'" content="删除" placement="top-start">
                <el-button size="mini" type="danger" @click.stop="deleteItem(scope.row)"><i class="el-icon-delete" /></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </template>
        <template v-slot:footer>
          <pagination  :total="currentTotal" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getDataList" />
        </template>
      </search-table>
      <page-details :show="isShowDetails" @onHideDetails="onHideDetails" :data="checkedItem" :gameType="listQuery.game_id"/>
      <robot-details :show="isRobotShow" @onHideDetails="onHideDetails" :data="checkedItem"/>
    </div>
  </div>
</template>

<script>
import { throttle } from "@/utils/index";
import { getRoom, delRoom } from "@/api/game";
import {
  GAME_TYPE
} from '@/utils/enum'
import PageDetails from "./page-details";
import RobotDetails from "./robot-details";
import SearchTable from "@/components/SearchTable";
export default {
  name: "room",
  components: {
    PageDetails,
    SearchTable,
    RobotDetails
  },
  data () {
    return {
      GAME_TYPE,
      dataList: [],
      checkedItem: null,
      listLoading: false,
      isShowDetails: false,
      isRobotShow: false,
      currentTotal: 0,
      listQuery: {
        page: 1,
        size: 50,
        game_id:0
      },
    };
  },
  created () {
    // this.getDataList();
  },
  activated () {
    this.getDataList();
  },
  methods: {
    // 获取房间列表
    async getDataList (query) {
      let that = this,params=that.listQuery;
      if(query && !query.page){
        params.page=1
        params.size=50
      }
      that.dataList = [];
      that.checkedItem = null;
      that.listLoading = true;
      const response = await getRoom(params)
      if (response.code==1) {
        if (response.data.list) {
          response.data.list.forEach((item) => {
            item.isChecked = false;
            item.a_gold_change=that.$public.mathNumberDivideWan(item.a_gold_change)
            item.changepoker=[]
            item.leopard=[]
            item.newer=[]
            item.newbie=[]
            item.slotsAmount=[]
            item.slotsRtpshow=[]
            item.slotsHundred=[]
            item.newerchangekill=[]
            item.changeClose=[]
            item.rmkill=[]
            item.userRmkill=[]
            item.rmkillNum=0
            item.runwith=[]
            item.changekill=[]
            if(item.a_robot_changepoker){
              item.changepoker=that.changeToArr(item.a_robot_changepoker)
            }
            if(item.a_user_set){
              item.leopard=that.leopardToArr(item.a_user_set)
            }
            if(item.a_robot_changekill){
              item.changekill=that.rmkillToArr(item.a_robot_changekill)
            }
            if(item.a_user_newer){
              item.newer=item.a_user_newer.split(',').map((item,index)=>{
                if(index==1 || index==4){
                  return item=that.$public.mathNumberDivideWan(item)
                }else{
                  return item
                }
              })
            }
            if(item.a_robot_bring){
              item.a_robot_bring=item.a_robot_bring.split(',')
            }
            if(item.a_robot_newbie){
              item.newbie=item.a_robot_newbie.split(',')
            }
            if(item.a_user_newerchangekill){
              item.newerchangekill=that.changeToArr(item.a_user_newerchangekill)
            }
            if(item.a_slots_amount){
              item.slotsAmount=that.amountToArr(item.a_slots_amount)
            }
            if(item.a_slots_rtpshow){
              item.slotsRtpshow=that.rtpshowToArr(item.a_slots_rtpshow)
            }
            if(item.a_hundred_amount){
              item.slotsHundred=that.hundredToArr(item.a_hundred_amount)
            }
            if(item.a_robot_rmkillcontrol){
              item.rmkill=that.rmkillToArr(item.a_robot_rmkillcontrol)
              item.rmkillNum=that.rmkillDataRate(item.rmkill)
            }
            if(item.a_user_rmkillcontrol){
              item.userRmkill=item.a_user_rmkillcontrol.split(',')
            }
            if(item.a_bind_change){
              item.changeClose=that.changeToArr(item.a_bind_change)
            }
            if(item.a_robot_runwith){
              item.runwith=item.a_robot_runwith.split(';').map(second=>{
                return second=second.split(',')
              })
            }
          });
          that.currentTotal = response.data.total;
          that.dataList = response.data.list;
        }
      }else{
        that.$message.error(response.msg)
      }
      that.listLoading = false;
    },
    // 编辑
    editItem (row) {
      let that = this;
      that.isShowDetails = true;
      that.checkedItem = row;
      that.handleCurrentChange(row);
    },
    // 删除
    deleteItem (row) {
      let that = this;
      that.handleCurrentChange(row);
      that.$confirm(`是否删除【${row.a_room_remark}】房间`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
          delRoom({
            id: row.id,
          }).then(function (res) {
            that.getDataList();
            that.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
      .catch(() => { });
    },
    // 游戏类型展示
    gameType(cellValue){
      if(cellValue!=3){
        return (this.GAME_TYPE.find((item) => item.value === cellValue)?.label || "-");
      }else{
        return '老虎机'
      }
    },
    changeToArr(str){
      let that=this,arr = that.$public.srtToArr(str), arrData = [];
      if (arr[0] && typeof (arr[0]) == 'string') {
        arrData.push({ min: Number(arr[0]), max: Number(arr[1]),chance:Number(arr[2])})
      } else {
        arr.map(item => {
          arrData.push({ min: Number(item[0]), max: Number(item[1]),chance:Number(item[2])})
        })
      }
      return arrData
    },
    leopardToArr(str){
      let that=this,arr = that.$public.srtToArr(str), arrData = [];
      if (arr[0] && typeof (arr[0]) == 'string') {
        arrData.push({ min: Number(arr[0]), max: Number(arr[1]),chance:Number(arr[2]),multiple:Number(arr[3])})
      } else {
        arr.map(item => {
          arrData.push({ min: Number(item[0]), max: Number(item[1]),chance:Number(item[2]),multiple:Number(item[3])})
        })
      }
      return arrData
    },
    rmkillToArr(str){
      let that=this,arr = that.$public.srtToArr(str), arrData = [];
      if (arr[0] && typeof (arr[0]) == 'string') {
        arrData.push({ round: Number(arr[0]),chance:Number(arr[1])})
      } else {
        arr.map(item => {
          arrData.push({ round: Number(item[0]),chance:Number(item[1])})
        })
      }
      return arrData
    },
    rmkillDataRate(arr){
      let reward=0;
      arr.map(item=>{
        reward+=item.chance
      })
      return reward
    },
    amountToArr(str){
      let that=this,arr = that.$public.srtToArr(str), arrData = [];
      if (arr[0] && typeof (arr[0]) == 'string') {
        arrData.push({ gears: Number(arr[0]),num:arr[1]})
      } else {
        arr.map(item => {
          arrData.push({ gears: Number(item[0]),num:item[1]})
        })
      }
      return arrData
    },
    rtpshowToArr(str){
      let that=this,arr = that.$public.srtToArr(str), arrData = [];
      arr.map(item => {
        arrData.push({num:item})
      })
      return arrData
    },
    hundredToArr(str){
      let that=this,arr = that.$public.srtToArr(str), arrData = [];
      arr.map(item => {
        arrData.push({num:that.$public.mathNumberDivideWan(item)})
      })
      return arrData
    },
    // 选择表格数据
    handleCurrentChange (val) {
      this.checkedItem = val;
      this.dataList.map((item) => {
        if (item.id === val.id) {
          item.isChecked = true;
        } else {
          item.isChecked = false;
        }
      });
    },
    // 隐藏详情
    onHideDetails (show) {
      this.isShowDetails = false;
      this.isRobotShow = false;
      if (show) {
        this.getDataList();
      }
    },
    // 刷新
    refresh: throttle(function () {
      this.listQuery = {
        page: 1,
        size: 50,
        game_id:0
      };
      this.getDataList();
    }, 2000),
  },
};
</script>

<style lang="scss" scoped></style>
